﻿using SimonGame.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using static SimonGame.Enum.LightEnum;

namespace SimonGame.Logic
{
    public class RoundColorLogic
    {
        private List<Light> lightList;
        private int currentIndex = -1, level = 1;
        private Timer timer;
        private PlaySimonGameViewModel viewModel;

        public int Speed { get; set; }

        public bool CanPlay { get; set; }


      
        public void Bian(object sender, EventArgs e)
        {
          
        }

        public RoundColorLogic()
        {
           
        }

        public RoundColorLogic(PlaySimonGameViewModel model)
        {
            this.timer = new Timer();
            this.timer.Elapsed += Timer_Elapsed;
            this.Speed = 1;
            this.viewModel = model;
            this.lightList = new List<Light>();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            currentIndex++;
            if (currentIndex == lightList.Count)
            {
                this.viewModel.Update(Light.None, 500);
                this.viewModel.Status = "PLAY!";
                currentIndex = -1;
                this.timer.Stop();
                this.CanPlay = true;
                return;
            }
            this.viewModel.Update(lightList[currentIndex], 500);
        }

        /// <summary>
        /// 开始游戏
        /// </summary>
        public void Start()
        {
            this.Start(this.level);
        }

        public void Start(int level)
        {
            this.Init(level);
            this.viewModel.Status = "WATCH!";
            this.viewModel.Restart = Visibility.Collapsed;
            this.viewModel.Score = level - 1;
            this.currentIndex = -1;
            timer.Interval = Speed * 1000;
            timer.Start();
            this.CanPlay = false;
        }

       

        /// <summary>
        /// 添加随机颜色
        /// </summary>
        /// <param name="level"></param>
        private void Init(int level)
        {
            for (int i = 0; i < level - lightList.Count; i++)
            {
                var hashCode = Guid.NewGuid().GetHashCode();
                int index = new Random(hashCode).Next(1,5);
                lightList.Add((Light)index);
            }
        }

        /// <summary>
        /// 点击图片
        /// </summary>
        public void Click(Light light)
        {
            if (!this.CanPlay)
                return;
            currentIndex++;
            var result = this.lightList[currentIndex] == light;
            if (result)
            {
                this.viewModel.Update(light, 200);
                if (currentIndex == this.lightList.Count - 1)
                {
                    level++;
                    this.Start(level);
                    return;
                }
            }
            else
            {
                this.viewModel.Status = "";
                this.viewModel.Restart = Visibility.Visible;
                this.level = 1;
                this.lightList.Clear();
                this.CanPlay = false;
            }
        }
    }
}
